﻿using Module_Debug.Hardware;
using System;
using System.Collections.Generic;
using System.IO;
using System.IO.Ports;
using System.Text;
using System.Threading;
using System.Windows.Forms;

namespace Module_Debug.Visionox_Gamma
{
    public partial class RM69350_Gamma : Form
    {
        CaSDK CaSDKObj = new CaSDK();
        CRC16 CRCData = new CRC16();
        Gamma_Addr GAMMAs = new Gamma_Addr();

        private bool Listening = false;//是否没有执行完invoke相关操作   
        private bool IClosing = false;//是否正在关闭串口，执行Application.DoEvents，并阻止再次invoke 

        private List<byte> buffer = new List<byte>(2048);//默认分配1/16页内存，并始终限制不允许超过

        private double[,] D_lums;//目标值-单色
        private double[] D_lum;//目标值-白色
        private double D_x, D_y;
        private double lum_off, x_off, y_off;
        private int index = 0;
        private int flag1 = 1; //lum
        private int flag2 = 1; //x
        private int flag3 = 1; //y
        private byte[] Send_buf = new byte[1];
        private byte[] Rec_tmp = new byte[1];
        public byte RM350_0XFE_XX = 0;
        private static bool Gamma_adjust_timeout_flag = false;

        private ushort[] mygamma_r = new ushort[28];
        private ushort[] mygamma_g = new ushort[28];
        private ushort[] mygamma_b = new ushort[28];

        //监听RM69350挂载
        frmMain frmHelp = null;

        public RM69350_Gamma(frmMain RM69350)
        {
            frmHelp = RM69350;
            InitializeComponent();
            frmHelp.Child_window_hide_already = true;
            frmHelp.Child_Gamma_Window_Open_Enable = false;//防止重复开启子窗体            

        }
        //VT5100 监听卸载
        private void RM69350_Gamma_FormClosed(object sender, FormClosedEventArgs e)
        {
            frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM69350_DataReceived);
            frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM69350_Gamma_FormClosed！\r\n";
            frmHelp.Child_Gamma_Window_Open_Enable = true; //允许其他子窗体打开
            frmHelp.Child_window_hide_already = false;
            frmHelp.IC_comboBox.Enabled = true;
        }

        private void RM69350_Gamma_VisibleChanged(object sender, EventArgs e)
        {
            if (frmHelp.Child_window_hide_already == false) //子窗体已开启标识
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(RM69350_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM69350_Gamma_FormHide！\r\n";
                frmHelp.Child_window_hide_already = true; //子窗体已隐藏标识
            }
            else
            {
                frmHelp.comPort.DataReceived -= new SerialDataReceivedEventHandler(frmHelp.SerialPort1_DataReceived);
                frmHelp.comPort.DataReceived += new SerialDataReceivedEventHandler(RM69350_DataReceived);
                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM69350_Gamma_FormOpened！\r\n";
                frmHelp.Child_window_hide_already = false; //子窗体已开启标识
            }
        }



        //串口接收
        private void RM69350_DataReceived(object sender, System.IO.Ports.SerialDataReceivedEventArgs e)//Getting data from Comm Port
        {
            if (IClosing) return;
            try
            {
                Listening = true;//设置标记，说明我已经开始处理数据，一会儿要使用系统UI的。         
                int n = frmHelp.comPort.BytesToRead;
                byte[] buf = new byte[n];
                byte data;

                frmHelp.comPort.Read(buf, 0, n);//读取缓冲数据     
                bool data_1_catched = false;//缓存记录数据是否捕获到    
                buffer.AddRange(buf);
                byte CRCH, CRCL;
                if (buffer.Count > 3)
                {
                    //0x3C: RM69350的Gamma读写、寄存器读写、OTP读写    设置  返回
                    if (buffer[0] == 0x3C && Setting.TempRecord.cmdLen > 0)
                    {
                        if (buffer.Count >= Setting.TempRecord.cmdLen)
                        {
                            Rec_tmp = new byte[Setting.TempRecord.cmdLen];
                            byte[] crcTemp = new byte[Setting.TempRecord.cmdLen];
                            buffer.CopyTo(0, crcTemp, 0, Setting.TempRecord.cmdLen);

                            CRCData.CalculateCrc16(crcTemp, out CRCH, out CRCL);
                            if (buffer[Setting.TempRecord.cmdLen - 2] != CRCH && buffer[Setting.TempRecord.cmdLen - 1] != CRCL)     //CRC校验出错
                            {
                                buffer.RemoveRange(0, buffer.Count);
                            }
                            else
                            {
                                buffer.CopyTo(0, Rec_tmp, 0, Setting.TempRecord.cmdLen);//复制一条完整数据到具体的数据缓存   
                                data_1_catched = true;
                                buffer.RemoveRange(0, Setting.TempRecord.cmdLen);//正确分析一条数据，从缓存中移除数据。 
                            }

                        }
                    }
                    else
                    {
                        buffer.RemoveRange(0, buffer.Count);
                    }
                }
                if (data_1_catched)
                {
                    this.Invoke((EventHandler)(delegate
                    {
                        frmHelp.T1.Enabled = false; //停止超时计数
                        if (Rec_tmp[0] == 0x3C)      //RM69350
                        {
                            switch (Rec_tmp[1])
                            {
                                case 0x01:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写寄存器\tOK！\r\n";
                                    }
                                    break;
                                case 0x02:
                                    {
                                        if (Setting.TempRecord.RM69350CmdType == 0x01)  //读普通寄存器
                                        {
                                            string tmp = "";
                                            tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                            R69350_Reg_Data.Text = tmp;
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "读寄存器\tOK！\r\n";
                                        }
                                    }
                                    break;

                                case 0x08:
                                    {
                                        if (Rec_tmp[4] == 0x00)
                                        {
                                            if ((Rec_tmp[3] & 0xf0) == 0x00)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x40)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t写 OK！\r\n";
                                            else if ((Rec_tmp[3] & 0xf0) == 0x80)
                                                frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t写 OK！\r\n";
                                        }
                                    }
                                    break;
                                case 0x0A:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma1 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma1 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0D:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma2 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma2 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0E:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma3 OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Gamma3 OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0F:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写GOA OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写GOA OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x06:
                                    {
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP ALL\t OK！\r\n";
                                    }
                                    break;
                                case 0x10:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Power OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Power OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x0B:
                                    {
                                        string tmp = "";
                                        tmp = String.Format("{0:X2}", Rec_tmp[4]);
                                        RM69350_OTP_Times.Text = tmp;
                                        frmHelp.textBox1.Text = frmHelp.textBox1.Text + "OTP\t读 OK！\r\n";
                                    }
                                    break;
                                case 0x0C:
                                    {
                                        if (Rec_tmp[3] == 0x00)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Others OTP\t写 OK！\r\n";
                                        else if (Rec_tmp[3] == 0x02)
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "写Others OTP\t写 ERROR！\r\n";
                                    }
                                    break;
                                case 0x11:
                                    {
                                        switch (Rec_tmp[3])
                                        {
                                            case 0x01:
                                            case 0x02:
                                            case 0x03:
                                                {
                                                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "Gamma切换成功！\r\n";
                                                    if (rbt_RM69350_gamma_28points.Checked == true)
                                                    {
                                                        rm350_R2.Enabled = true; rm350_G2.Enabled = true; rm350_B2.Enabled = true;
                                                        rm350_R19.Enabled = true; rm350_G19.Enabled = true; rm350_B19.Enabled = true;
                                                        rm350_R23.Enabled = true; rm350_G23.Enabled = true; rm350_B23.Enabled = true;
                                                        rm350_R27.Enabled = true; rm350_G27.Enabled = true; rm350_B27.Enabled = true;
                                                        rm350_R35.Enabled = true; rm350_G35.Enabled = true; rm350_B35.Enabled = true;
                                                        rm350_R39.Enabled = true; rm350_G39.Enabled = true; rm350_B39.Enabled = true;
                                                        rm350_R55.Enabled = true; rm350_G55.Enabled = true; rm350_B55.Enabled = true;
                                                        rm350_R79.Enabled = true; rm350_G79.Enabled = true; rm350_B79.Enabled = true;
                                                        rm350_R111.Enabled = true; rm350_G111.Enabled = true; rm350_B111.Enabled = true;
                                                        rm350_R143.Enabled = true; rm350_G143.Enabled = true; rm350_B143.Enabled = true;
                                                        rm350_R175.Enabled = true; rm350_G175.Enabled = true; rm350_B175.Enabled = true;
                                                        rm350_R207.Enabled = true; rm350_G207.Enabled = true; rm350_B207.Enabled = true;
                                                        rm350_R239.Enabled = true; rm350_G239.Enabled = true; rm350_B239.Enabled = true;
                                                    }
                                                    else
                                                    {
                                                        rm350_R2.Enabled = false; rm350_G2.Enabled = false; rm350_B2.Enabled = false;
                                                        rm350_R19.Enabled = false; rm350_G19.Enabled = false; rm350_B19.Enabled = false;
                                                        rm350_R23.Enabled = false; rm350_G23.Enabled = false; rm350_B23.Enabled = false;
                                                        rm350_R27.Enabled = false; rm350_G27.Enabled = false; rm350_B27.Enabled = false;
                                                        rm350_R35.Enabled = false; rm350_G35.Enabled = false; rm350_B35.Enabled = false;
                                                        rm350_R39.Enabled = false; rm350_G39.Enabled = false; rm350_B39.Enabled = false;
                                                        rm350_R55.Enabled = false; rm350_G55.Enabled = false; rm350_B55.Enabled = false;
                                                        rm350_R79.Enabled = false; rm350_G79.Enabled = false; rm350_B79.Enabled = false;
                                                        rm350_R111.Enabled = false; rm350_G111.Enabled = false; rm350_B111.Enabled = false;
                                                        rm350_R143.Enabled = false; rm350_G143.Enabled = false; rm350_B143.Enabled = false;
                                                        rm350_R175.Enabled = false; rm350_G175.Enabled = false; rm350_B175.Enabled = false;
                                                        rm350_R207.Enabled = false; rm350_G207.Enabled = false; rm350_B207.Enabled = false;
                                                        rm350_R239.Enabled = false; rm350_G239.Enabled = false; rm350_B239.Enabled = false;
                                                    }

                                                }

                                                break;
                                            default: break;
                                        }

                                    }
                                    break;
                                case 0x09:
                                    {
                                        if (Setting.TempRecord.RM69350CmdType == 0x03)
                                        {
                                            rm350_R0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            rm350_R1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            rm350_R2.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            rm350_R3.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            rm350_R7.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            rm350_R11.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            rm350_R15.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            rm350_R19.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            rm350_R23.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            rm350_R27.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            rm350_R31.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            rm350_R35.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            rm350_R39.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            rm350_R47.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            rm350_R55.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            rm350_R63.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            rm350_R79.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            rm350_R95.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            rm350_R111.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            rm350_R127.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            rm350_R143.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            rm350_R159.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            rm350_R175.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            rm350_R191.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            rm350_R207.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            rm350_R223.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            rm350_R239.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            rm350_R255.Value = Decimal.Parse((Rec_tmp[58] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());

                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "红 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM69350CmdType == 0x04)
                                        {
                                            rm350_G0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            rm350_G1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            rm350_G2.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            rm350_G3.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            rm350_G7.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            rm350_G11.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            rm350_G15.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            rm350_G19.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            rm350_G23.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            rm350_G27.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            rm350_G31.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            rm350_G35.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            rm350_G39.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            rm350_G47.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            rm350_G55.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            rm350_G63.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            rm350_G79.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            rm350_G95.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            rm350_G111.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            rm350_G127.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            rm350_G143.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            rm350_G159.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            rm350_G175.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            rm350_G191.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            rm350_G207.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            rm350_G223.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            rm350_G239.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            rm350_G255.Value = Decimal.Parse((Rec_tmp[58] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "绿 Gamma数据\t读 OK！\r\n";
                                        }
                                        else if (Setting.TempRecord.RM69350CmdType == 0x05)
                                        {
                                            rm350_B0.Value = Decimal.Parse((Rec_tmp[4] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[5].ToString());
                                            rm350_B1.Value = Decimal.Parse((Rec_tmp[6] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[7].ToString());
                                            rm350_B2.Value = Decimal.Parse((Rec_tmp[8] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[9].ToString());
                                            rm350_B3.Value = Decimal.Parse((Rec_tmp[10] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[11].ToString());
                                            rm350_B7.Value = Decimal.Parse((Rec_tmp[12] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[13].ToString());
                                            rm350_B11.Value = Decimal.Parse((Rec_tmp[14] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[15].ToString());
                                            rm350_B15.Value = Decimal.Parse((Rec_tmp[16] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[17].ToString());
                                            rm350_B19.Value = Decimal.Parse((Rec_tmp[18] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[19].ToString());
                                            rm350_B23.Value = Decimal.Parse((Rec_tmp[20] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[21].ToString());
                                            rm350_B27.Value = Decimal.Parse((Rec_tmp[22] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[23].ToString());
                                            rm350_B31.Value = Decimal.Parse((Rec_tmp[24] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[25].ToString());
                                            rm350_B35.Value = Decimal.Parse((Rec_tmp[26] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[27].ToString());
                                            rm350_B39.Value = Decimal.Parse((Rec_tmp[28] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[29].ToString());
                                            rm350_B47.Value = Decimal.Parse((Rec_tmp[30] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[31].ToString());
                                            rm350_B55.Value = Decimal.Parse((Rec_tmp[32] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[33].ToString());
                                            rm350_B63.Value = Decimal.Parse((Rec_tmp[34] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[35].ToString());
                                            rm350_B79.Value = Decimal.Parse((Rec_tmp[36] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[37].ToString());
                                            rm350_B95.Value = Decimal.Parse((Rec_tmp[38] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[39].ToString());
                                            rm350_B111.Value = Decimal.Parse((Rec_tmp[40] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[41].ToString());
                                            rm350_B127.Value = Decimal.Parse((Rec_tmp[42] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[43].ToString());
                                            rm350_B143.Value = Decimal.Parse((Rec_tmp[44] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[45].ToString());
                                            rm350_B159.Value = Decimal.Parse((Rec_tmp[46] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[47].ToString());
                                            rm350_B175.Value = Decimal.Parse((Rec_tmp[48] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[49].ToString());
                                            rm350_B191.Value = Decimal.Parse((Rec_tmp[50] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[51].ToString());
                                            rm350_B207.Value = Decimal.Parse((Rec_tmp[52] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[53].ToString());
                                            rm350_B223.Value = Decimal.Parse((Rec_tmp[54] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[55].ToString());
                                            rm350_B239.Value = Decimal.Parse((Rec_tmp[56] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[57].ToString());
                                            rm350_B255.Value = Decimal.Parse((Rec_tmp[58] & 0x07).ToString()) * 256 + Decimal.Parse(Rec_tmp[59].ToString());
                                            frmHelp.textBox1.Text = frmHelp.textBox1.Text + "蓝 Gamma数据\t读 OK！\r\n";
                                        }

                                    }
                                    break;
                                default: break;

                            }
                        }
                        frmHelp.textBox1.SelectionStart = this.frmHelp.textBox1.Text.Length;
                        frmHelp.textBox1.ScrollToCaret();//滚动到光标处
                        if (frmHelp.textBox1.Lines.Length > 50) frmHelp.textBox1.Text = null;
                    }));
                }
            }
            finally
            {
                Listening = false;
            }
        }

        //Gamma保存
        private void RM69350_SaveGamma_Click(object sender, EventArgs e)
        {
            SaveFileDialog sf = new SaveFileDialog(); //对话框
            sf.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; //定义保存的文件的类型
            string str = "保存Gamma文件";
            if (sf.ShowDialog() == DialogResult.OK) //如果确定保存
            {
                if (sf.FileName == "") return; //如果没有输入文件名
                try
                {
                    StreamWriter sw = null; //文件流
                    sw = new StreamWriter(sf.FileName, false, Encoding.UTF8);
                    str = str + "R0," + rm350_R0.Text + "," + "G0," + rm350_G0.Text + "," + "B0," + rm350_B0.Text + "\r\n";
                    str = str + "R1," + rm350_R1.Text + "," + "G1," + rm350_G1.Text + "," + "B1," + rm350_B1.Text + "\r\n";
                    str = str + "R2," + rm350_R2.Text + "," + "G2," + rm350_G2.Text + "," + "B2," + rm350_B2.Text + "\r\n";
                    str = str + "R3," + rm350_R3.Text + "," + "G3," + rm350_G3.Text + "," + "B3," + rm350_B3.Text + "\r\n";
                    str = str + "R7," + rm350_R7.Text + "," + "G7," + rm350_G7.Text + "," + "B7," + rm350_B7.Text + "\r\n";
                    str = str + "R11," + rm350_R11.Text + "," + "G11," + rm350_G11.Text + "," + "B11," + rm350_B11.Text + "\r\n";
                    str = str + "R15," + rm350_R15.Text + "," + "G15," + rm350_G15.Text + "," + "B15," + rm350_B15.Text + "\r\n";
                    str = str + "R19," + rm350_R19.Text + "," + "G19," + rm350_G19.Text + "," + "B19," + rm350_B19.Text + "\r\n";
                    str = str + "R23," + rm350_R23.Text + "," + "G23," + rm350_G23.Text + "," + "B23," + rm350_B23.Text + "\r\n";
                    str = str + "R27," + rm350_R27.Text + "," + "G27," + rm350_G27.Text + "," + "B27," + rm350_B27.Text + "\r\n";
                    str = str + "R31," + rm350_R31.Text + "," + "G31," + rm350_G31.Text + "," + "B31," + rm350_B31.Text + "\r\n";
                    str = str + "R35," + rm350_R35.Text + "," + "G35," + rm350_G35.Text + "," + "B35," + rm350_B35.Text + "\r\n";
                    str = str + "R39," + rm350_R39.Text + "," + "G39," + rm350_G39.Text + "," + "B39," + rm350_B39.Text + "\r\n";
                    str = str + "R47," + rm350_R47.Text + "," + "G47," + rm350_G47.Text + "," + "B47," + rm350_B47.Text + "\r\n";
                    str = str + "R55," + rm350_R55.Text + "," + "G55," + rm350_G55.Text + "," + "B55," + rm350_B55.Text + "\r\n";
                    str = str + "R63," + rm350_R63.Text + "," + "G63," + rm350_G63.Text + "," + "B63," + rm350_B63.Text + "\r\n";
                    str = str + "R79," + rm350_R79.Text + "," + "G79," + rm350_G79.Text + "," + "B79," + rm350_B79.Text + "\r\n";
                    str = str + "R95," + rm350_R95.Text + "," + "G95," + rm350_G95.Text + "," + "B95," + rm350_B95.Text + "\r\n";
                    str = str + "R111," + rm350_R111.Text + "," + "G111," + rm350_G111.Text + "," + "B111," + rm350_B111.Text + "\r\n";
                    str = str + "R127," + rm350_R127.Text + "," + "G127," + rm350_G127.Text + "," + "B127," + rm350_B127.Text + "\r\n";
                    str = str + "R143," + rm350_R143.Text + "," + "G143," + rm350_G143.Text + "," + "B143," + rm350_B143.Text + "\r\n";
                    str = str + "R159," + rm350_R159.Text + "," + "G159," + rm350_G159.Text + "," + "B159," + rm350_B159.Text + "\r\n";
                    str = str + "R175," + rm350_R175.Text + "," + "G175," + rm350_G175.Text + "," + "B175," + rm350_B175.Text + "\r\n";
                    str = str + "R191," + rm350_R191.Text + "," + "G191," + rm350_G191.Text + "," + "B191," + rm350_B191.Text + "\r\n";
                    str = str + "R207," + rm350_R207.Text + "," + "G207," + rm350_G207.Text + "," + "B207," + rm350_B207.Text + "\r\n";
                    str = str + "R223," + rm350_R223.Text + "," + "G223," + rm350_G223.Text + "," + "B223," + rm350_B223.Text + "\r\n";
                    str = str + "R239," + rm350_R239.Text + "," + "G239," + rm350_G239.Text + "," + "B239," + rm350_B239.Text + "\r\n";
                    str = str + "R255," + rm350_R255.Text + "," + "G255," + rm350_G255.Text + "," + "B255," + rm350_B255.Text + "\r\n";
                    sw.WriteLine(str);
                    sw.Dispose(); //释放资源
                    sw.Close(); //关闭数据流
                    MessageBox.Show("Gamma保存OK！", "系统提示：");
                }
                catch
                {
                    MessageBox.Show("Gamma保存时发生错误，请再试！", "系统提示：");
                }
            }
        } //
        //Gamma载入
        private void RM69350_LoadGamma_Click(object sender, EventArgs e)
        {
            OpenFileDialog of = new OpenFileDialog();
            string strtmp = "";
            of.Filter = @"csv文件(*.csv)|*.csv|所有文件(*.*)|*.*"; ;
            of.Title = "打开Gamma文件";

            if (of.ShowDialog() == DialogResult.OK)
            {
                //读取文本信息
                FileStream fs = new FileStream(of.FileName, FileMode.Open, FileAccess.Read);
                StreamReader streamReader = new StreamReader(fs, Encoding.GetEncoding("GBK"));
                streamReader.BaseStream.Seek(0, SeekOrigin.Begin);
                string strLine = streamReader.ReadLine();
                //循环读取文本信息
                while (!string.IsNullOrEmpty(strLine))
                {
                    strtmp = strtmp + strLine + ",";
                    strLine = streamReader.ReadLine(); //循环读取下一行
                }
                fs.Close(); //关闭流
                streamReader.Close();
                string[] arrtmp = strtmp.Split(new Char[] { ',' });
                rm350_R0.Text = arrtmp[1].ToString(); rm350_G0.Text = arrtmp[3].ToString(); rm350_B0.Text = arrtmp[5].ToString();
                rm350_R1.Text = arrtmp[7].ToString(); rm350_G1.Text = arrtmp[9].ToString(); rm350_B1.Text = arrtmp[11].ToString();
                rm350_R2.Text = arrtmp[13].ToString(); rm350_G2.Text = arrtmp[15].ToString(); rm350_B2.Text = arrtmp[17].ToString();
                rm350_R3.Text = arrtmp[19].ToString(); rm350_G3.Text = arrtmp[21].ToString(); rm350_B3.Text = arrtmp[23].ToString();
                rm350_R7.Text = arrtmp[25].ToString(); rm350_G7.Text = arrtmp[27].ToString(); rm350_B7.Text = arrtmp[29].ToString();
                rm350_R11.Text = arrtmp[31].ToString(); rm350_G11.Text = arrtmp[33].ToString(); rm350_B11.Text = arrtmp[35].ToString();
                rm350_R15.Text = arrtmp[37].ToString(); rm350_G15.Text = arrtmp[39].ToString(); rm350_B15.Text = arrtmp[41].ToString();
                rm350_R19.Text = arrtmp[43].ToString(); rm350_G19.Text = arrtmp[45].ToString(); rm350_B19.Text = arrtmp[47].ToString();
                rm350_R23.Text = arrtmp[49].ToString(); rm350_G23.Text = arrtmp[51].ToString(); rm350_B23.Text = arrtmp[53].ToString();
                rm350_R27.Text = arrtmp[55].ToString(); rm350_G27.Text = arrtmp[57].ToString(); rm350_B27.Text = arrtmp[59].ToString();
                rm350_R31.Text = arrtmp[61].ToString(); rm350_G31.Text = arrtmp[63].ToString(); rm350_B31.Text = arrtmp[65].ToString();
                rm350_R35.Text = arrtmp[67].ToString(); rm350_G35.Text = arrtmp[69].ToString(); rm350_B35.Text = arrtmp[71].ToString();
                rm350_R39.Text = arrtmp[73].ToString(); rm350_G39.Text = arrtmp[75].ToString(); rm350_B39.Text = arrtmp[77].ToString();
                rm350_R47.Text = arrtmp[79].ToString(); rm350_G47.Text = arrtmp[81].ToString(); rm350_B47.Text = arrtmp[83].ToString();
                rm350_R55.Text = arrtmp[85].ToString(); rm350_G55.Text = arrtmp[87].ToString(); rm350_B55.Text = arrtmp[89].ToString();
                rm350_R63.Text = arrtmp[91].ToString(); rm350_G63.Text = arrtmp[93].ToString(); rm350_B63.Text = arrtmp[95].ToString();
                rm350_R79.Text = arrtmp[97].ToString(); rm350_G79.Text = arrtmp[99].ToString(); rm350_B79.Text = arrtmp[101].ToString();
                rm350_R95.Text = arrtmp[103].ToString(); rm350_G95.Text = arrtmp[105].ToString(); rm350_B95.Text = arrtmp[107].ToString();
                rm350_R111.Text = arrtmp[109].ToString(); rm350_G111.Text = arrtmp[111].ToString(); rm350_B111.Text = arrtmp[113].ToString();
                rm350_R127.Text = arrtmp[115].ToString(); rm350_G127.Text = arrtmp[117].ToString(); rm350_B127.Text = arrtmp[119].ToString();
                rm350_R143.Text = arrtmp[121].ToString(); rm350_G143.Text = arrtmp[123].ToString(); rm350_B143.Text = arrtmp[125].ToString();
                rm350_R159.Text = arrtmp[127].ToString(); rm350_G159.Text = arrtmp[129].ToString(); rm350_B159.Text = arrtmp[131].ToString();
                rm350_R175.Text = arrtmp[133].ToString(); rm350_G175.Text = arrtmp[135].ToString(); rm350_B175.Text = arrtmp[137].ToString();
                rm350_R191.Text = arrtmp[139].ToString(); rm350_G191.Text = arrtmp[141].ToString(); rm350_B191.Text = arrtmp[143].ToString();
                rm350_R207.Text = arrtmp[145].ToString(); rm350_G207.Text = arrtmp[147].ToString(); rm350_B207.Text = arrtmp[149].ToString();
                rm350_R223.Text = arrtmp[151].ToString(); rm350_G223.Text = arrtmp[153].ToString(); rm350_B223.Text = arrtmp[155].ToString();
                rm350_R239.Text = arrtmp[157].ToString(); rm350_G239.Text = arrtmp[159].ToString(); rm350_B239.Text = arrtmp[161].ToString();
                rm350_R255.Text = arrtmp[163].ToString(); rm350_G255.Text = arrtmp[165].ToString(); rm350_B255.Text = arrtmp[167].ToString();
            }
        }

        private void radio_Gamma3_CheckedChanged(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x3C;
            Send_buf[2] = 0x02;

            RadioButton btnTmp = (RadioButton)sender;
            Setting.TempRecord.RM69350CmdType = 0x06;       //      6
            if (RM350_radioGamma1.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x01;

            }
            else if (RM350_radioGamma2.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x02;
            }
            else if (RM350_radioGamma3.Checked == true)
            {
                Send_buf[1] = 0x11;
                Send_buf[3] = 0x03;
            }
            if (rbt_RM69350_gamma_28points.Checked == true)
            {
                Send_buf[4] = 0x01;
            }
            else
            {
                Send_buf[4] = 0x02;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                RM350_0XFE_XX = 0x00;
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void RM350_Others_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x3C;        //RM69350
            Send_buf[2] = 0x02;
            switch (btnTmp.Name)
            {
                case "RM69350_Reg_R":       //              2
                    {
                        RM350_0XFE_XX = 0x00;
                        Setting.TempRecord.RM69350CmdType = 0x01;   //读普通寄存器    1
                        Send_buf[1] = 0x02;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(R69350_Reg_Addr.Text.Trim(), 16)).ToString());
                    }
                    break;
                case "RM69350_51Reg_W":       //              2
                    {

                        Send_buf[1] = 0x51;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(R69350_Reg_Addr.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(R69350_Reg_Data.Text.Trim(), 16)).ToString());
                        CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
                        frmHelp.comPort.Write(Send_buf, 0, 7);
                        return;
                    }
                    break;
                case "RM69350_Reg_W":         //w写寄存器   1
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x01;
                        Send_buf[3] = byte.Parse((Convert.ToInt32(R69350_Reg_Addr.Text.Trim(), 16)).ToString());
                        Send_buf[4] = byte.Parse((Convert.ToInt32(R69350_Reg_Data.Text.Trim(), 16)).ToString());
                    }
                    break;
                case "RM69350_R_OTPTime":       //          B
                    {
                        RM350_0XFE_XX = 0x00;
                        Send_buf[1] = 0x0B;
                        Setting.TempRecord.RM69350CmdType = 0x02;       //      2
                        if (rbt_RM69350_Gamma1.Checked == true)
                        {
                            Send_buf[3] = 0x00;
                        }
                        else if (rbt_RM69350_Gamma2.Checked == true)
                        {
                            Send_buf[3] = 0x01;
                        }
                        else if (rbt_RM69350_Gamma3.Checked == true)
                        {
                            Send_buf[3] = 0x02;
                        }
                        else if (rbt_RM69350_Power.Checked == true)
                        {
                            Send_buf[3] = 0x03;
                        }
                        else if (rbt_RM69350_GOA.Checked == true)
                        {
                            Send_buf[3] = 0x04;
                        }
                    }
                    break;
                case "RM69350_W_OTPTime":         //w写寄存器   C
                    {
                        RM350_0XFE_XX = 0x00;
                        if (Convert.ToDecimal(RM69350_OTP_Times.Text) > 3)
                        {
                            MessageBox.Show("OTP次数不能大于3次", "警告");
                            return;
                        }
                        else
                        {
                            Send_buf[1] = 0x0C;
                            Send_buf[4] = byte.Parse((Convert.ToInt32(RM69350_OTP_Times.Text.Trim(), 16)).ToString());
                        }
                    }
                    break;

                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void RM350_Gamma_Click(object sender, EventArgs e)
        {
            Button btnTmp = (Button)sender;
            Send_buf = new byte[62];
            Setting.TempRecord.cmdLen = 62;
            Send_buf[2] = 57;
            Send_buf[3] = 0;
            Send_buf[0] = 0x3C; //RM69350


            switch (btnTmp.Name)
            {
                case "RM69350_GammaR_Red":
                    {
                        Setting.TempRecord.RM69350CmdType = 0x03;       //      3
                        Send_buf[1] = 0x09;                             //读Gamma
                        Send_buf[3] |= 0x00;                            //红色red
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[4] = 0x00;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[4] = 0x01;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[4] = 0x02;
                        }
                    }
                    break;
                case "RM69350_GammaR_Green":
                    {
                        Setting.TempRecord.RM69350CmdType = 0x04;   //      4
                        Send_buf[1] = 0x09;                         //  读Gamma
                        Send_buf[3] |= 0x42;                        //绿色 green
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[4] = 0x00;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[4] = 0x01;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[4] = 0x02;
                        }
                    }
                    break;
                case "RM69350_GammaR_Blue": //              9
                    {
                        Setting.TempRecord.RM69350CmdType = 0x05;   //      5
                        Send_buf[1] = 0x09;                         //   读Gamma
                        Send_buf[3] |= 0x81;                        //   blue
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[4] = 0x00;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[4] = 0x01;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[4] = 0x02;
                        }
                    }
                    break;
                //--------------------------------------
                case "RM69350_GammaW_Red":
                    {
                        Send_buf[1] = 0x08;                       //写Gamma
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[3] = 0x00;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[3] = 0x01;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[3] = 0x02;
                        }
                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(rm350_R0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(rm350_R0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(rm350_R1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(rm350_R1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(rm350_R2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(rm350_R2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(rm350_R3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(rm350_R3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(rm350_R7.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(rm350_R7.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(rm350_R11.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(rm350_R11.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(rm350_R15.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(rm350_R15.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(rm350_R19.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(rm350_R19.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(rm350_R23.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(rm350_R23.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(rm350_R27.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(rm350_R27.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(rm350_R31.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(rm350_R31.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(rm350_R35.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(rm350_R35.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(rm350_R39.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(rm350_R39.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(rm350_R47.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(rm350_R47.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(rm350_R55.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(rm350_R55.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(rm350_R63.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(rm350_R63.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(rm350_R79.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(rm350_R79.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(rm350_R95.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(rm350_R95.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(rm350_R111.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(rm350_R111.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(rm350_R127.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(rm350_R127.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(rm350_R143.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(rm350_R143.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(rm350_R159.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(rm350_R159.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(rm350_R175.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(rm350_R175.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(rm350_R191.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(rm350_R191.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(rm350_R207.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(rm350_R207.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(rm350_R223.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(rm350_R223.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(rm350_R239.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(rm350_R239.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(rm350_R255.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(rm350_R255.Value.ToString())) & 0xff);
                    }
                    break;
                case "RM69350_GammaW_Green":
                    {
                        Send_buf[1] = 0x08;
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[3] = 0x40;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[3] = 0x41;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[3] = 0x42;
                        }
                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(rm350_G0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(rm350_G0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(rm350_G1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(rm350_G1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(rm350_G2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(rm350_G2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(rm350_G3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(rm350_G3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(rm350_G7.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(rm350_G7.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(rm350_G11.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(rm350_G11.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(rm350_G15.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(rm350_G15.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(rm350_G19.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(rm350_G19.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(rm350_G23.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(rm350_G23.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(rm350_G27.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(rm350_G27.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(rm350_G31.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(rm350_G31.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(rm350_G35.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(rm350_G35.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(rm350_G39.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(rm350_G39.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(rm350_G47.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(rm350_G47.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(rm350_G55.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(rm350_G55.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(rm350_G63.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(rm350_G63.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(rm350_G79.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(rm350_G79.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(rm350_G95.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(rm350_G95.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(rm350_G111.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(rm350_G111.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(rm350_G127.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(rm350_G127.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(rm350_G143.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(rm350_G143.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(rm350_G159.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(rm350_G159.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(rm350_G175.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(rm350_G175.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(rm350_G191.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(rm350_G191.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(rm350_G207.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(rm350_G207.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(rm350_G223.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(rm350_G223.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(rm350_G239.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(rm350_G239.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(rm350_G255.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(rm350_G255.Value.ToString())) & 0xff);
                    }
                    break;
                case "RM69350_GammaW_Blue":     //      8
                    {
                        Send_buf[1] = 0x08;
                        if (RM350_radioGamma1.Checked == true)
                        {
                            Send_buf[3] = 0x80;
                        }
                        else if (RM350_radioGamma2.Checked == true)
                        {
                            Send_buf[3] = 0x81;
                        }
                        else if (RM350_radioGamma3.Checked == true)
                        {
                            Send_buf[3] = 0x82;
                        }

                        Send_buf[4] = Convert.ToByte(Convert.ToUInt16(rm350_B0.Value.ToString()) >> 8);
                        Send_buf[5] = Convert.ToByte((Convert.ToUInt16(rm350_B0.Value.ToString())) & 0xff);
                        Send_buf[6] = Convert.ToByte(Convert.ToUInt16(rm350_B1.Value.ToString()) >> 8);
                        Send_buf[7] = Convert.ToByte((Convert.ToUInt16(rm350_B1.Value.ToString())) & 0xff);
                        Send_buf[8] = Convert.ToByte(Convert.ToUInt16(rm350_B2.Value.ToString()) >> 8);
                        Send_buf[9] = Convert.ToByte((Convert.ToUInt16(rm350_B2.Value.ToString())) & 0xff);
                        Send_buf[10] = Convert.ToByte(Convert.ToUInt16(rm350_B3.Value.ToString()) >> 8);
                        Send_buf[11] = Convert.ToByte((Convert.ToUInt16(rm350_B3.Value.ToString())) & 0xff);
                        Send_buf[12] = Convert.ToByte(Convert.ToUInt16(rm350_B7.Value.ToString()) >> 8);
                        Send_buf[13] = Convert.ToByte((Convert.ToUInt16(rm350_B7.Value.ToString())) & 0xff);
                        Send_buf[14] = Convert.ToByte(Convert.ToUInt16(rm350_B11.Value.ToString()) >> 8);
                        Send_buf[15] = Convert.ToByte((Convert.ToUInt16(rm350_B11.Value.ToString())) & 0xff);
                        Send_buf[16] = Convert.ToByte(Convert.ToUInt16(rm350_B15.Value.ToString()) >> 8);
                        Send_buf[17] = Convert.ToByte((Convert.ToUInt16(rm350_B15.Value.ToString())) & 0xff);
                        Send_buf[18] = Convert.ToByte(Convert.ToUInt16(rm350_B19.Value.ToString()) >> 8);
                        Send_buf[19] = Convert.ToByte((Convert.ToUInt16(rm350_B19.Value.ToString())) & 0xff);
                        Send_buf[20] = Convert.ToByte(Convert.ToUInt16(rm350_B23.Value.ToString()) >> 8);
                        Send_buf[21] = Convert.ToByte((Convert.ToUInt16(rm350_B23.Value.ToString())) & 0xff);
                        Send_buf[22] = Convert.ToByte(Convert.ToUInt16(rm350_B27.Value.ToString()) >> 8);
                        Send_buf[23] = Convert.ToByte((Convert.ToUInt16(rm350_B27.Value.ToString())) & 0xff);
                        Send_buf[24] = Convert.ToByte(Convert.ToUInt16(rm350_B31.Value.ToString()) >> 8);
                        Send_buf[25] = Convert.ToByte((Convert.ToUInt16(rm350_B31.Value.ToString())) & 0xff);
                        Send_buf[26] = Convert.ToByte(Convert.ToUInt16(rm350_B35.Value.ToString()) >> 8);
                        Send_buf[27] = Convert.ToByte((Convert.ToUInt16(rm350_B35.Value.ToString())) & 0xff);
                        Send_buf[28] = Convert.ToByte(Convert.ToUInt16(rm350_B39.Value.ToString()) >> 8);
                        Send_buf[29] = Convert.ToByte((Convert.ToUInt16(rm350_B39.Value.ToString())) & 0xff);
                        Send_buf[30] = Convert.ToByte(Convert.ToUInt16(rm350_B47.Value.ToString()) >> 8);
                        Send_buf[31] = Convert.ToByte((Convert.ToUInt16(rm350_B47.Value.ToString())) & 0xff);
                        Send_buf[32] = Convert.ToByte(Convert.ToUInt16(rm350_B55.Value.ToString()) >> 8);
                        Send_buf[33] = Convert.ToByte((Convert.ToUInt16(rm350_B55.Value.ToString())) & 0xff);
                        Send_buf[34] = Convert.ToByte(Convert.ToUInt16(rm350_B63.Value.ToString()) >> 8);
                        Send_buf[35] = Convert.ToByte((Convert.ToUInt16(rm350_B63.Value.ToString())) & 0xff);
                        Send_buf[36] = Convert.ToByte(Convert.ToUInt16(rm350_B79.Value.ToString()) >> 8);
                        Send_buf[37] = Convert.ToByte((Convert.ToUInt16(rm350_B79.Value.ToString())) & 0xff);
                        Send_buf[38] = Convert.ToByte(Convert.ToUInt16(rm350_B95.Value.ToString()) >> 8);
                        Send_buf[39] = Convert.ToByte((Convert.ToUInt16(rm350_B95.Value.ToString())) & 0xff);
                        Send_buf[40] = Convert.ToByte(Convert.ToUInt16(rm350_B111.Value.ToString()) >> 8);
                        Send_buf[41] = Convert.ToByte((Convert.ToUInt16(rm350_B111.Value.ToString())) & 0xff);
                        Send_buf[42] = Convert.ToByte(Convert.ToUInt16(rm350_B127.Value.ToString()) >> 8);
                        Send_buf[43] = Convert.ToByte((Convert.ToUInt16(rm350_B127.Value.ToString())) & 0xff);
                        Send_buf[44] = Convert.ToByte(Convert.ToUInt16(rm350_B143.Value.ToString()) >> 8);
                        Send_buf[45] = Convert.ToByte((Convert.ToUInt16(rm350_B143.Value.ToString())) & 0xff);
                        Send_buf[46] = Convert.ToByte(Convert.ToUInt16(rm350_B159.Value.ToString()) >> 8);
                        Send_buf[47] = Convert.ToByte((Convert.ToUInt16(rm350_B159.Value.ToString())) & 0xff);
                        Send_buf[48] = Convert.ToByte(Convert.ToUInt16(rm350_B175.Value.ToString()) >> 8);
                        Send_buf[49] = Convert.ToByte((Convert.ToUInt16(rm350_B175.Value.ToString())) & 0xff);
                        Send_buf[50] = Convert.ToByte(Convert.ToUInt16(rm350_B191.Value.ToString()) >> 8);
                        Send_buf[51] = Convert.ToByte((Convert.ToUInt16(rm350_B191.Value.ToString())) & 0xff);
                        Send_buf[52] = Convert.ToByte(Convert.ToUInt16(rm350_B207.Value.ToString()) >> 8);
                        Send_buf[53] = Convert.ToByte((Convert.ToUInt16(rm350_B207.Value.ToString())) & 0xff);
                        Send_buf[54] = Convert.ToByte(Convert.ToUInt16(rm350_B223.Value.ToString()) >> 8);
                        Send_buf[55] = Convert.ToByte((Convert.ToUInt16(rm350_B223.Value.ToString())) & 0xff);
                        Send_buf[56] = Convert.ToByte(Convert.ToUInt16(rm350_B239.Value.ToString()) >> 8);
                        Send_buf[57] = Convert.ToByte((Convert.ToUInt16(rm350_B239.Value.ToString())) & 0xff);
                        Send_buf[58] = Convert.ToByte(Convert.ToUInt16(rm350_B255.Value.ToString()) >> 8);
                        Send_buf[59] = Convert.ToByte((Convert.ToUInt16(rm350_B255.Value.ToString())) & 0xff);

                    }
                    break;
                default:
                    break;
            }
            CRCData.CalculateCrc16(Send_buf, out Send_buf[60], out Send_buf[61]);
            try
            {
                RM350_0XFE_XX = 0x00;
                frmHelp.comPort.Write(Send_buf, 0, 62);   //RM69350
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void RM350_OTP_Click(object sender, EventArgs e)
        {
            Send_buf = new byte[7];
            Button btnTmp = (Button)sender;
            Setting.TempRecord.cmdLen = 7;

            Send_buf[0] = 0x3C;        //RM69350
            Send_buf[1] = 0x0A;
            Send_buf[2] = 0x02;

            if (Convert.ToDecimal(RM69350_OTP_Times.Text) > 3)
            {
                MessageBox.Show("OTP次数不能大于3次", "警告");
                return;
            }
            if (rbt_RM69350_Gamma1.Checked == true)
            {
                Send_buf[3] = 0x00;
            }
            else if (rbt_RM69350_Gamma2.Checked == true)
            {
                Send_buf[3] = 0x01;
            }
            else if (rbt_RM69350_Gamma3.Checked == true)
            {
                Send_buf[3] = 0x02;
            }
            else if (rbt_RM69350_Power.Checked == true)
            {
                Send_buf[3] = 0x03;
            }
            else if (rbt_RM69350_GOA.Checked == true)
            {
                Send_buf[3] = 0x04;
            }
            else if (rbt_RM69350_SPR.Checked == true)
            {
                Send_buf[3] = 0x05;
            }
            else if (rbt_RM69350_OTP_All.Checked == true)
            {
                Send_buf[3] = 0x06;
            }
            Send_buf[4] = Convert.ToByte(RM69350_OTP_Times.Text);
            CRCData.CalculateCrc16(Send_buf, out Send_buf[5], out Send_buf[6]);
            try
            {
                frmHelp.comPort.Write(Send_buf, 0, 7);
            }
            catch
            {
                MessageBox.Show("串口有误，请检查", "错误");
            }
            frmHelp.T1.Enabled = false;
            frmHelp.T1.Interval = 2000;
            frmHelp.T1.Enabled = true;
        }
        private void btn350Stop_Click(object sender, EventArgs e)
        {
            T20.Enabled = false;
            flag1 = 1;
            flag2 = 1;
            flag3 = 1;
        }
        private void btnAutogamma350_Click(object sender, EventArgs e)
        {
            int flag = 1;
            double Lr_d, Lg_d, Lb_d;
            D_lums = new double[27, 3];
            D_lum = new double[27];
            index = 0;
            //------------flexible gamma use------------------
            frmHelp.Gamma_auto_adjust_count = 0;
            Gamma_adjust_timeout_flag = false;
            //------------------------------------------------
            D_lum[0] = Convert.ToDouble(txt_350_255.Text);
            D_lum[1] = Convert.ToDouble(txt_350_239.Text);
            D_lum[2] = Convert.ToDouble(txt_350_223.Text);
            D_lum[3] = Convert.ToDouble(txt_350_207.Text);
            D_lum[4] = Convert.ToDouble(txt_350_191.Text);
            D_lum[5] = Convert.ToDouble(txt_350_175.Text);
            D_lum[6] = Convert.ToDouble(txt_350_159.Text);
            D_lum[7] = Convert.ToDouble(txt_350_143.Text);
            D_lum[8] = Convert.ToDouble(txt_350_127.Text);
            D_lum[9] = Convert.ToDouble(txt_350_111.Text);
            D_lum[10] = Convert.ToDouble(txt_350_95.Text);
            D_lum[11] = Convert.ToDouble(txt_350_79.Text);
            D_lum[12] = Convert.ToDouble(txt_350_63.Text);
            D_lum[13] = Convert.ToDouble(txt_350_55.Text);
            D_lum[14] = Convert.ToDouble(txt_350_47.Text);
            D_lum[15] = Convert.ToDouble(txt_350_39.Text);
            D_lum[16] = Convert.ToDouble(txt_350_35.Text);
            D_lum[17] = Convert.ToDouble(txt_350_31.Text);
            D_lum[18] = Convert.ToDouble(txt_350_27.Text);
            D_lum[19] = Convert.ToDouble(txt_350_23.Text);
            D_lum[20] = Convert.ToDouble(txt_350_19.Text);
            D_lum[21] = Convert.ToDouble(txt_350_15.Text);
            D_lum[22] = Convert.ToDouble(txt_350_11.Text);
            D_lum[23] = Convert.ToDouble(txt_350_7.Text);
            D_lum[24] = Convert.ToDouble(txt_350_3.Text);
            D_lum[25] = Convert.ToDouble(txt_350_2.Text);
            D_lum[26] = Convert.ToDouble(txt_350_1.Text);
            D_x = Convert.ToDouble(txt_350_x.Text);
            D_y = Convert.ToDouble(txt_350_y.Text);

            CaSDKObj.YUV_to_RGB(D_y, D_x, out Lr_d, out Lb_d, out Lg_d);

            for (int i = 0; i < 27; i++)
            {
                D_lums[i, 0] = D_lum[i] * Lr_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 1] = D_lum[i] * Lg_d / (Lr_d + Lg_d + Lb_d);
                D_lums[i, 2] = D_lum[i] * Lb_d / (Lr_d + Lg_d + Lb_d);
            }
            mygamma_r = GAMMAs.RM350_Gamma_r;
            mygamma_g = GAMMAs.RM350_Gamma_g;
            mygamma_b = GAMMAs.RM350_Gamma_b;

            R69350_Reg_Addr.Text = "FE";
            R69350_Reg_Data.Text = "51";
            RM350_Others_Click(RM69350_Reg_W, e);

            switch (flag)
            {
                case 1://寄存器值与亮度正比
                    {
                        lum_off = Convert.ToDouble(txt_350_loff.Text);
                        x_off = Convert.ToDouble(txt_350_xoff.Text);
                        y_off = Convert.ToDouble(txt_350_yoff.Text);
                        Application.DoEvents();
                        frmHelp.OLED_Show_Click(frmHelp.btnwhite, e);
                        T20.Enabled = true;
                    }
                    break;
                case 2://寄存器值与亮度反比
                    break;
                default:
                    break;
            }
        }

        //RM69350
        private int[] Parttern_RM69350 = new int[27] { 255, 239, 223, 207, 191, 175, 159, 143, 127, 111, 95, 79, 63, 55, 47, 39, 35, 31, 27, 23, 19, 15, 11, 7, 3, 2, 1 };

        private void T20_Tick(object sender, EventArgs e)
        {
            RM69350_1D_gamma(null, null);
        }

        private void btnCalculate350_Click(object sender, EventArgs e)
        {
            txt_350_255.Text = (Math.Pow((Parttern_RM69350[0] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_239.Text = (Math.Pow((Parttern_RM69350[1] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_223.Text = (Math.Pow((Parttern_RM69350[2] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_207.Text = (Math.Pow((Parttern_RM69350[3] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_191.Text = (Math.Pow((Parttern_RM69350[4] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_175.Text = (Math.Pow((Parttern_RM69350[5] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_159.Text = (Math.Pow((Parttern_RM69350[6] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_143.Text = (Math.Pow((Parttern_RM69350[7] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_127.Text = (Math.Pow((Parttern_RM69350[8] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_111.Text = (Math.Pow((Parttern_RM69350[9] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_95.Text = (Math.Pow((Parttern_RM69350[10] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_79.Text = (Math.Pow((Parttern_RM69350[11] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_63.Text = (Math.Pow((Parttern_RM69350[12] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_55.Text = (Math.Pow((Parttern_RM69350[13] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_47.Text = (Math.Pow((Parttern_RM69350[14] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_39.Text = (Math.Pow((Parttern_RM69350[15] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_35.Text = (Math.Pow((Parttern_RM69350[16] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_31.Text = (Math.Pow((Parttern_RM69350[17] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_27.Text = (Math.Pow((Parttern_RM69350[18] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_23.Text = (Math.Pow((Parttern_RM69350[19] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_19.Text = (Math.Pow((Parttern_RM69350[20] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_15.Text = (Math.Pow((Parttern_RM69350[21] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_11.Text = (Math.Pow((Parttern_RM69350[22] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_7.Text = (Math.Pow((Parttern_RM69350[23] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_3.Text = (Math.Pow((Parttern_RM69350[24] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_2.Text = (Math.Pow((Parttern_RM69350[25] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
            txt_350_1.Text = (Math.Pow((Parttern_RM69350[26] / 255.0), Convert.ToDouble(txt_350_gamma.Text)) * Convert.ToDouble(txt_350_l.Text)).ToString("F4");
        }
        private void RM69350_1D_gamma(Object myObject, EventArgs myEventArgs)
        {
            double Lr_t, Lg_t, Lb_t, lumr, lumg, lumb;
            frmHelp.Msr_CASDK();

            if ((frmHelp.CASDK_LUM < (D_lum[index] + lum_off)) && (frmHelp.CASDK_LUM > (D_lum[index] - lum_off)))//G
                flag1 = 0;
            if ((frmHelp.CASDK_y < (D_y + y_off)) && (frmHelp.CASDK_y > (D_y - y_off)) && flag1 == 0)//B
                flag2 = 0;
            if ((frmHelp.CASDK_x < (D_x + x_off)) && (frmHelp.CASDK_x > (D_x - x_off)) && flag1 == 0 && flag2 == 0)//R
                flag3 = 0;

            if (flag1 == 0 && flag2 == 0 && flag3 == 0)
            {
                T20.Enabled = false;
                //reset-----
                Gamma_adjust_timeout_flag = false;
                frmHelp.Gamma_auto_adjust_count = 0;
                //--------
                index++;
                if (index == 27)
                {
                    frmHelp.textBox1.Text = frmHelp.textBox1.Text + "RM69350 gamma调试完毕!\r\n";
                }
                if (index < 27)
                {
                    switch (index)
                    {
                        case 1:
                            lum_off = Convert.ToDouble(txt_350_239off.Text);
                            break;
                        case 2:
                            lum_off = Convert.ToDouble(txt_350_223off.Text);
                            break;
                        case 3:
                            lum_off = Convert.ToDouble(txt_350_207off.Text);
                            break;
                        case 4:
                            lum_off = Convert.ToDouble(txt_350_191off.Text);
                            break;
                        case 5:
                            lum_off = Convert.ToDouble(txt_350_175off.Text);
                            break;
                        case 6:
                            lum_off = Convert.ToDouble(txt_350_159off.Text);
                            break;
                        case 7:
                            lum_off = Convert.ToDouble(txt_350_143off.Text);
                            break;
                        case 8:
                            lum_off = Convert.ToDouble(txt_350_127off.Text) * Convert.ToDouble(txt_350_127.Text) / 100;
                            break;
                        case 9:
                            lum_off = Convert.ToDouble(txt_350_111off.Text) * Convert.ToDouble(txt_350_111.Text) / 100;
                            break;
                        case 10:
                            lum_off = Convert.ToDouble(txt_350_95off.Text) * Convert.ToDouble(txt_350_95.Text) / 100;
                            break;
                        case 11:
                            lum_off = Convert.ToDouble(txt_350_79off.Text) * Convert.ToDouble(txt_350_79.Text) / 100;
                            break;
                        case 12:
                            lum_off = Convert.ToDouble(txt_350_63off.Text) * Convert.ToDouble(txt_350_63.Text) / 100;
                            break;
                        case 13:
                            lum_off = Convert.ToDouble(txt_350_55off.Text) * Convert.ToDouble(txt_350_55.Text) / 100;
                            break;
                        case 14:
                            lum_off = Convert.ToDouble(txt_350_47off.Text) * Convert.ToDouble(txt_350_47.Text) / 100;
                            break;
                        case 15:
                            lum_off = Convert.ToDouble(txt_350_39off.Text) * Convert.ToDouble(txt_350_39.Text) / 100;
                            break;
                        case 16:
                            lum_off = Convert.ToDouble(txt_350_35off.Text) * Convert.ToDouble(txt_350_35.Text) / 100;
                            break;
                        case 17:
                            lum_off = Convert.ToDouble(txt_350_31off.Text) * Convert.ToDouble(txt_350_31.Text) / 100;
                            break;
                        case 18:
                            lum_off = Convert.ToDouble(txt_350_27off.Text) * Convert.ToDouble(txt_350_27.Text) / 100;
                            break;
                        case 19:
                            lum_off = Convert.ToDouble(txt_350_23off.Text) * Convert.ToDouble(txt_350_23.Text) / 100;
                            break;
                        case 20:
                            lum_off = Convert.ToDouble(txt_350_19off.Text) * Convert.ToDouble(txt_350_19.Text) / 100;
                            break;
                        case 21:
                            {
                                lum_off = Convert.ToDouble(txt_350_15off.Text) * Convert.ToDouble(txt_350_15.Text) / 100;
                            }
                            break;
                        case 22:
                            {
                                lum_off = Convert.ToDouble(txt_350_11off.Text) * Convert.ToDouble(txt_350_11.Text) / 100;
                            }
                            break;
                        case 23:
                            {
                                lum_off = Convert.ToDouble(txt_350_7off.Text) * Convert.ToDouble(txt_350_7.Text) / 100;
                            }
                            break;
                        case 24:
                            {
                                lum_off = Convert.ToDouble(txt_350_3off.Text) * Convert.ToDouble(txt_350_3.Text) / 100;
                            }
                            break;
                        case 25:
                            {
                                lum_off = Convert.ToDouble(txt_350_2off.Text) * Convert.ToDouble(txt_350_2.Text) / 100;
                            }
                            break;
                        case 26:
                            {
                                lum_off = Convert.ToDouble(txt_350_1off.Text) * Convert.ToDouble(txt_350_1.Text) / 100;
                            }
                            break;
                    }

                    frmHelp.numPatternR.Value = Parttern_RM69350[index];
                    frmHelp.numPatternG.Value = Parttern_RM69350[index];
                    frmHelp.numPatternB.Value = Parttern_RM69350[index];
                    Application.DoEvents();
                    frmHelp.OLED_Show_Click(frmHelp.btnPatternW, null);//写入Pattern参数
                    Thread.Sleep(150); //1秒=1000毫秒，
                    T20.Enabled = true;
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
            else
            {
                decimal r, g, b;
                int flexible_gamma_count = 120;

                CaSDKObj.YUV_to_RGB(frmHelp.CASDK_y, frmHelp.CASDK_x, out Lr_t, out Lb_t, out Lg_t);

                lumr = frmHelp.CASDK_LUM * Lr_t / (Lr_t + Lg_t + Lb_t);
                lumg = frmHelp.CASDK_LUM * Lg_t / (Lr_t + Lg_t + Lb_t);
                lumb = frmHelp.CASDK_LUM * Lb_t / (Lr_t + Lg_t + Lb_t);
                try
                {
                    switch (index)
                    {
                        case 0:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R255.Value, rm350_G255.Value, rm350_B255.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R255.Value = r;
                                rm350_G255.Value = g;
                                rm350_B255.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[27], Convert.ToUInt16(rm350_R255.Value), mygamma_g[27], Convert.ToUInt16(rm350_G255.Value), mygamma_b[27], Convert.ToUInt16(rm350_B255.Value));
                            }
                            break;
                        case 1:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R239.Value, rm350_G239.Value, rm350_B239.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R239.Value = r;
                                rm350_G239.Value = g;
                                rm350_B239.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[26], Convert.ToUInt16(rm350_R239.Value), mygamma_g[26], Convert.ToUInt16(rm350_G239.Value), mygamma_b[26], Convert.ToUInt16(rm350_B239.Value));
                            }
                            break;
                        case 2:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R223.Value, rm350_G223.Value, rm350_B223.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R223.Value = r;
                                rm350_G223.Value = g;
                                rm350_B223.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[25], Convert.ToUInt16(rm350_R223.Value), mygamma_g[25], Convert.ToUInt16(rm350_G223.Value), mygamma_b[25], Convert.ToUInt16(rm350_B223.Value));
                            }
                            break;
                        case 3:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R207.Value, rm350_G207.Value, rm350_B207.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R207.Value = r;
                                rm350_G207.Value = g;
                                rm350_B207.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[24], Convert.ToUInt16(rm350_R207.Value), mygamma_g[24], Convert.ToUInt16(rm350_G207.Value), mygamma_b[24], Convert.ToUInt16(rm350_B207.Value));
                            }
                            break;
                        case 4:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R191.Value, rm350_G191.Value, rm350_B191.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R191.Value = r;
                                rm350_G191.Value = g;
                                rm350_B191.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[23], Convert.ToUInt16(rm350_R191.Value), mygamma_g[23], Convert.ToUInt16(rm350_G191.Value), mygamma_b[23], Convert.ToUInt16(rm350_B191.Value));
                            }
                            break;
                        case 5:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R175.Value, rm350_G175.Value, rm350_B175.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R175.Value = r;
                                rm350_G175.Value = g;
                                rm350_B175.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[22], Convert.ToUInt16(rm350_R175.Value), mygamma_g[22], Convert.ToUInt16(rm350_G175.Value), mygamma_b[22], Convert.ToUInt16(rm350_B175.Value));
                            }
                            break;
                        case 6:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R159.Value, rm350_G159.Value, rm350_B159.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R159.Value = r;
                                rm350_G159.Value = g;
                                rm350_B159.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[21], Convert.ToUInt16(rm350_R159.Value), mygamma_g[21], Convert.ToUInt16(rm350_G159.Value), mygamma_b[21], Convert.ToUInt16(rm350_B159.Value));
                            }
                            break;
                        case 7:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R143.Value, rm350_G143.Value, rm350_B143.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R143.Value = r;
                                rm350_G143.Value = g;
                                rm350_B143.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[20], Convert.ToUInt16(rm350_R143.Value), mygamma_g[20], Convert.ToUInt16(rm350_G143.Value), mygamma_b[20], Convert.ToUInt16(rm350_B143.Value));
                            }
                            break;
                        case 8:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R127.Value, rm350_G127.Value, rm350_B127.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R127.Value = r;
                                rm350_G127.Value = g;
                                rm350_B127.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[19], Convert.ToUInt16(rm350_R127.Value), mygamma_g[19], Convert.ToUInt16(rm350_G127.Value), mygamma_b[19], Convert.ToUInt16(rm350_B127.Value));
                            }
                            break;
                        case 9:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R111.Value, rm350_G111.Value, rm350_B111.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R111.Value = r;
                                rm350_G111.Value = g;
                                rm350_B111.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[18], Convert.ToUInt16(rm350_R111.Value), mygamma_g[18], Convert.ToUInt16(rm350_G111.Value), mygamma_b[18], Convert.ToUInt16(rm350_B111.Value));
                            }
                            break;
                        case 10:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R95.Value, rm350_G95.Value, rm350_B95.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R95.Value = r;
                                rm350_G95.Value = g;
                                rm350_B95.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[17], Convert.ToUInt16(rm350_R95.Value), mygamma_g[17], Convert.ToUInt16(rm350_G95.Value), mygamma_b[17], Convert.ToUInt16(rm350_B95.Value));
                            }
                            break;
                        case 11:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R79.Value, rm350_G79.Value, rm350_B79.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R79.Value = r;
                                rm350_G79.Value = g;
                                rm350_B79.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[16], Convert.ToUInt16(rm350_R79.Value), mygamma_g[16], Convert.ToUInt16(rm350_G79.Value), mygamma_b[16], Convert.ToUInt16(rm350_B79.Value));
                            }
                            break;
                        case 12:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R63.Value, rm350_G63.Value, rm350_B63.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R63.Value = r;
                                rm350_G63.Value = g;
                                rm350_B63.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[15], Convert.ToUInt16(rm350_R63.Value), mygamma_g[15], Convert.ToUInt16(rm350_G63.Value), mygamma_b[15], Convert.ToUInt16(rm350_B63.Value));
                            }
                            break;
                        case 13:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R55.Value, rm350_G55.Value, rm350_B55.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R55.Value = r;
                                rm350_G55.Value = g;
                                rm350_B55.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[14], Convert.ToUInt16(rm350_R55.Value), mygamma_g[14], Convert.ToUInt16(rm350_G55.Value), mygamma_b[14], Convert.ToUInt16(rm350_B55.Value));
                            }
                            break;
                        case 14:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R47.Value, rm350_G47.Value, rm350_B47.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R47.Value = r;
                                rm350_G47.Value = g;
                                rm350_B47.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[13], Convert.ToUInt16(rm350_R47.Value), mygamma_g[13], Convert.ToUInt16(rm350_G47.Value), mygamma_b[13], Convert.ToUInt16(rm350_B47.Value));
                            }
                            break;
                        case 15:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R39.Value, rm350_G39.Value, rm350_B39.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R39.Value = r;
                                rm350_G39.Value = g;
                                rm350_B39.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[12], Convert.ToUInt16(rm350_R39.Value), mygamma_g[12], Convert.ToUInt16(rm350_G39.Value), mygamma_b[12], Convert.ToUInt16(rm350_B39.Value));
                            }
                            break;
                        case 16:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R35.Value, rm350_G35.Value, rm350_B35.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R35.Value = r;
                                rm350_G35.Value = g;
                                rm350_B35.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[11], Convert.ToUInt16(rm350_R35.Value), mygamma_g[11], Convert.ToUInt16(rm350_G35.Value), mygamma_b[11], Convert.ToUInt16(rm350_B35.Value));
                            }
                            break;
                        case 17:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R31.Value, rm350_G31.Value, rm350_B31.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R31.Value = r;
                                rm350_G31.Value = g;
                                rm350_B31.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[10], Convert.ToUInt16(rm350_R31.Value), mygamma_g[10], Convert.ToUInt16(rm350_G31.Value), mygamma_b[10], Convert.ToUInt16(rm350_B31.Value));
                            }
                            break;
                        case 18:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R27.Value, rm350_G27.Value, rm350_B27.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R27.Value = r;
                                rm350_G27.Value = g;
                                rm350_B27.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[9], Convert.ToUInt16(rm350_R27.Value), mygamma_g[9], Convert.ToUInt16(rm350_G27.Value), mygamma_b[9], Convert.ToUInt16(rm350_B27.Value));
                            }
                            break;
                        case 19:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R23.Value, rm350_G23.Value, rm350_B23.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R23.Value = r;
                                rm350_G23.Value = g;
                                rm350_B23.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[8], Convert.ToUInt16(rm350_R23.Value), mygamma_g[8], Convert.ToUInt16(rm350_G23.Value), mygamma_b[8], Convert.ToUInt16(rm350_B23.Value));
                            }
                            break;
                        case 20:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R19.Value, rm350_G19.Value, rm350_B19.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R19.Value = r;
                                rm350_G19.Value = g;
                                rm350_B19.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[7], Convert.ToUInt16(rm350_R19.Value), mygamma_g[7], Convert.ToUInt16(rm350_G19.Value), mygamma_b[7], Convert.ToUInt16(rm350_B19.Value));
                            }
                            break;
                        case 21:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R15.Value, rm350_G15.Value, rm350_B15.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R15.Value = r;
                                rm350_G15.Value = g;
                                rm350_B15.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[6], Convert.ToUInt16(rm350_R15.Value), mygamma_g[6], Convert.ToUInt16(rm350_G15.Value), mygamma_b[6], Convert.ToUInt16(rm350_B15.Value));
                            }
                            break;
                        case 22:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R11.Value, rm350_G11.Value, rm350_B11.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R11.Value = r;
                                rm350_G11.Value = g;
                                rm350_B11.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[5], Convert.ToUInt16(rm350_R11.Value), mygamma_g[5], Convert.ToUInt16(rm350_G11.Value), mygamma_b[5], Convert.ToUInt16(rm350_B11.Value));
                            }
                            break;
                        case 23:
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R7.Value, rm350_G7.Value, rm350_B7.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R7.Value = r;
                                rm350_G7.Value = g;
                                rm350_B7.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[4], Convert.ToUInt16(rm350_R7.Value), mygamma_g[4], Convert.ToUInt16(rm350_G7.Value), mygamma_b[4], Convert.ToUInt16(rm350_B7.Value));
                            }
                            break;
                        case 24://20阶以下
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R3.Value, rm350_G3.Value, rm350_B3.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R3.Value = r;
                                rm350_G3.Value = g;
                                rm350_B3.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[3], Convert.ToUInt16(rm350_R3.Value), mygamma_g[3], Convert.ToUInt16(rm350_G3.Value), mygamma_b[3], Convert.ToUInt16(rm350_B3.Value));
                            }
                            break;
                        case 25://
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R2.Value, rm350_G2.Value, rm350_B2.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R2.Value = r;
                                rm350_G2.Value = g;
                                rm350_B2.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[2], Convert.ToUInt16(rm350_R2.Value), mygamma_g[2], Convert.ToUInt16(rm350_G2.Value), mygamma_b[2], Convert.ToUInt16(rm350_B2.Value));
                            }
                            break;
                        case 26://
                            {
                                frmHelp.Flexible_Gamma_auto_Adujst(flag1, flag3, flag2, lumg, lumr, lumb, D_lums[index, 1], D_lums[index, 0], D_lums[index, 2], rm350_R1.Value, rm350_G1.Value, rm350_B1.Value, Parttern_RM69350[index], 0x7ff, out r, out g, out b, out Gamma_adjust_timeout_flag);
                                if (Gamma_adjust_timeout_flag == true)
                                {
                                    flag1 = 0; flag3 = 0; flag2 = 0;
                                }
                                rm350_R1.Value = r;
                                rm350_G1.Value = g;
                                rm350_B1.Value = b;
                                frmHelp.auto_gamma_write(mygamma_r[1], Convert.ToUInt16(rm350_R1.Value), mygamma_g[1], Convert.ToUInt16(rm350_G1.Value), mygamma_b[1], Convert.ToUInt16(rm350_B1.Value));
                            }
                            break;
                        case 27://
                            {
                                Gamma_adjust_timeout_flag = true;
                                rm350_R0.Value = 0;
                                rm350_G0.Value = 0;
                                rm350_B0.Value = 0;
                                Application.DoEvents();
                            }
                            break;

                    }
                }
                catch
                {
                    T20.Enabled = false;
                    MessageBox.Show("数值超限！");
                    flag1 = 1;
                    flag2 = 1;
                    flag3 = 1;
                }
            }
        }
        private void RM69350_Gamma_Load(object sender, EventArgs e)
        {

        }
    }
}
